var closeEvent = new CustomEvent('event_close', { detail: 'close windows' });
async function openWindow(url,options,params) {
    let winOptions = {
        width: 1000,
        height: 600,
        left: 0,
        top: 0,
        winName:'child'
    }
    $.extend(winOptions, options);
    let winOption = "width=" + winOptions.width + ",height=" + winOptions.height + ",top=" + winOptions.top + ",left=" + winOptions.left
        + ",menubar=no,toolbar=no,location=no,scrollbars=no,status=no,resizable=no,location=no"

    window.childWindow = window.open(url, winOptions.winName, winOption)
    addCss()
    window.childWindow.onload = function(){
        window.childWindow.onunload = unloadCommon;
        window.childWindow.focus()
    }
    window.onfocus = focusCommon;
    let res = await openWindowReturn(window);
    console.log('openWindow:'+res)
    return res;
}

function openWindowReturn(window) {
    return new Promise(function (resolve, reject) {
        window.childWindow.addEventListener('event_close', function (event) {
            var returnValue =  window.childWindow.returnValue
            resolve(returnValue);
        });
    });
}

function unloadCommon() {
    console.log('关闭窗口')
    //返回值
    // let value = callReturnCommon()
    // if (value) {
    //     window.opener.getCommon(value)
    // }
    // window.returnValue = value
    let curWindow = window
    console.log(curWindow.name)
    if(!curWindow.opener){
        // window.childWindow.onunload();
        curWindow = curWindow.childWindow
        // return;
    }
    curWindow.dispatchEvent(closeEvent)
    curWindow.opener.childWindow = undefined
    curWindow.opener.removeCss()
    if (curWindow.childWindow) {
        curWindow.childWindow.close()
    }
}

function focusCommon() {
    console.log('点击了父窗口')
    if (window.childWindow && typeof (window.childWindow) != "undefined") {
        setTimeout(function () {
            console.log('点击了父窗口，重新聚焦到子窗口------延时' + window.childWindow.name)
            window.blur()
            window.childWindow.focus()
            if (window.childWindow.childWindow && typeof (window.childWindow.childWindow) != "undefined") {
                setTimeout(function () {
                    console.log('点击了爷爷窗口，重新聚焦到孙窗口------延时' + window.childWindow.childWindow.name)
                    window.childWindow.blur()
                    window.childWindow.childWindow.focus()
                }, 60);
            }
        }, 50);
    }
}

function addCss() {
    console.log('添加模糊效果')
    $('body').addClass('content')
}
function removeCss() {
    console.log('移除模糊效果')
    $('body').removeClass('content')
}


function getDataFromWin(options) {

}
